﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using Billing.Windows;
using Billing.Utilities;

namespace Billing
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //Mouse.OverrideCursor = Cursors.None;
            txtUserID.Focus();
#if DEPLOYMENT
            cmbBranchSelected.Visibility = Visibility.Visible;
#else
            cmbBranchSelected.Visibility = Visibility.Collapsed;
#endif
        }

        private void MainWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (this.IsKeyboardFocusWithin)
                {
                    if (e.Key == Key.Return && txtPassword.Password != string.Empty && LoginConditions())
                    {
                        txtUserID.Clear();
                        txtPassword.Clear();
                        spMainLogin.Visibility = Visibility.Hidden;
                        mainBorder.Visibility = Visibility.Visible;
                        mainBorder1.Visibility = Visibility.Visible;
                        mainBorder2.Visibility = Visibility.Visible;
                        
                        mainBorder3.Visibility = Visibility.Visible;

                        brdrLogOut.Visibility = Visibility.Visible;
                        //TODO:Branch Deployment
                        cmbBranchSelected.Visibility = Visibility.Collapsed;
                        spMainLogin.IsEnabled = false;
                        CacheManager.LoadItems();
                    }
                    else if (e.Key == Key.L && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        if (MessageBoxResult.OK == MessageBox.Show("Do you want to LogOut?", "Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Question))
                        {
                            DataRemainderPopup data = new DataRemainderPopup();
                            data.Owner = this;
                            if (data.ShowDialog().Value)
                            {
                                spMainLogin.Visibility = Visibility.Visible;
                                mainBorder.Visibility = Visibility.Hidden;
                                mainBorder1.Visibility = Visibility.Hidden;
                                mainBorder2.Visibility = Visibility.Hidden;
                                mainBorder3.Visibility = Visibility.Hidden;
                                brdrLogOut.Visibility = Visibility.Hidden;
                                spMainLogin.IsEnabled = true;
                                txtUserID.Focus();
                                txtUserID.Clear();
                                txtPassword.Clear();
                                //TODO:Branch Deployment
#if DEPLOYMENT
                            cmbBranchSelected.Visibility = Visibility.Visible;
#else
                                cmbBranchSelected.Visibility = Visibility.Collapsed;
#endif
                                e.Handled = true;
                            }
                        }
                    }
                    else if (e.Key == Key.F1 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        while (true)
                        {
                            Storage.CreditNoteCompany = string.Empty;
                            Storage.CreditNoteID = 0;
                            BillingGrid mb = new BillingGrid();
                            mb.Owner = this;
                            mb.ShowDialog();
                            if (mb.IsExit)
                                break;

                        }
                    }
                    else if (e.Key == Key.F2 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        StockAllBranches cb = new StockAllBranches();
                        cb.Owner = this;
                        cb.ShowDialog();
                    }
                    else if (e.Key == Key.F3 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        CreditNotes cn = new CreditNotes();
                        cn.Owner = this;
                        cn.ShowDialog();
                    }
                    else if (e.Key == Key.F4 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        Expenditure exp = new Expenditure();
                        exp.Owner = this;
                        exp.ShowDialog();
                    }
                    else if (e.Key == Key.F5 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        DamageStock dm = new DamageStock();
                        dm.Owner = this;
                        dm.ShowDialog();
                    }
                    else if (e.Key == Key.F6 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        CakeBooking cb = new CakeBooking();
                        cb.Owner = this;
                        cb.ShowDialog();
                    }
                    else if (e.Key == Key.F7 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        BulkOrderMain bulk = new BulkOrderMain();
                        bulk.Owner = this;
                        bulk.ShowDialog();
                    }
                    else if (e.Key == Key.F8 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        Stock st = new Stock(WindowTypes.MainWindowF2);
                        st.Owner = this;
                        st.ShowDialog();
                    }
                    // Add Leave
                    else if (e.Key == Key.F9 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        while (true)
                        {
                            AddLeave leave = new AddLeave();
                            leave.Owner = this;
                            leave.ShowDialog();
                            if (leave.IsExit)
                                break;
                        }
                    }
                    // Add OT
                    else if ((e.Key == Key.F10 || e.SystemKey == Key.F10 && e.Key == Key.System) && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        while (true)
                        {
                            AddOT ot = new AddOT();
                            ot.Owner = this;
                            ot.ShowDialog();
                            if (ot.IsExit)
                                break;
                        }
                    }
                    // Add Advance
                    else if (e.Key == Key.F11 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        while (true)
                        {
                            AddAdvance advance = new AddAdvance();
                            advance.Owner = this;
                            advance.ShowDialog();
                            if (advance.IsExit)
                                break;
                        }
                    }
                    // Add Bills
                    else if (e.Key == Key.F12 && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        while (true)
                        {
                            AddBill ab = new AddBill();
                            ab.Owner = this;
                            ab.ShowDialog();
                            if (ab.IsExit)
                                break;
                        }
                    }
                    // Add Amounts
                    else if (e.Key == Key.F && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        Amounts amounts = new Amounts();
                        amounts.Owner = this;
                        amounts.ShowDialog();
                    }
                    // Transfer Data
                    else if (e.Key == Key.T && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        TransferData data = new TransferData();
                        data.Owner = this;
                        data.ShowDialog();
                    }
                    else if (e.Key == Key.Escape && this.IsActive && spMainLogin.Visibility == Visibility.Visible)
                    {
                        if (MessageBox.Show("Do you want to close this application?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Stop) == MessageBoxResult.Yes)
                            this.Close();
                    }
                    // Vendor Bills
                    else if (e.Key == Key.V && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        VendorBills cn = new VendorBills();
                        cn.Owner = this;
                        cn.ShowDialog();
                    }
                    // Payments
                    else if (e.Key == Key.P && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        Payment payment = new Payment();
                        payment.Owner = this;
                        payment.ShowDialog();
                    }
                    else if (e.Key == Key.O && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        Orders orders = new Orders();
                        orders.Owner = this;
                        orders.ShowDialog();
                    }
                    else if (e.Key == Key.I && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        InternetChecker internet = new InternetChecker();
                        internet.Owner = this;
                        internet.ShowDialog();
                    }
                    else if (e.Key == Key.S && spMainLogin.Visibility == Visibility.Hidden)
                    {
                        SyncRates syncRates = new SyncRates();
                        syncRates.Owner = this;
                        syncRates.ShowDialog();
                    }
                }
            }
            catch (Exception) { }
        }

        private bool LoginConditions()
        {
            try
            {
                if (txtUserID.Text != string.Empty && txtPassword.Password != string.Empty && txtUserID.Text != null && txtPassword.Password != null)
                {
                    //TODO:Branch Deployment
#if DEPLOYMENT
                    if (cmbBranchSelected.Visibility == Visibility.Visible)
                        Database.BranchSelected = int.Parse((cmbBranchSelected.SelectedItem as ComboBoxItem).Tag.ToString());
                    else
                        Database.BranchSelected = 0;
#endif
                    string empID = Database.ValidateUser(txtUserID.Text.Trim(), txtPassword.Password.Trim());
                    if (empID != string.Empty)
                    {
                        Storage.LoginUser = empID;
                        Storage.LoginUserName = txtUserID.Text.ToUpper();
                        Storage.IsFirstBill = true;
                        return true;
                    }
                    else
                    {
                        MessageBox.Show("Invalid Login Details", "Login Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
                        txtUserID.Text = string.Empty;
                        txtPassword.Password = string.Empty;
                        txtUserID.Focus();
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show("Enter both UserName & Password fields", "Invalid Login", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void txtUserID_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if ((e.Key == Key.Enter || e.Key == Key.Right) && txtUserID.Text.Trim() != string.Empty)
                {
                    txtPassword.Focus();
                }
            }
            catch (Exception) { }
        }

        private void txtPassword_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Left)
                {
                    txtUserID.Focus();
                    txtUserID.SelectAll();
                }
            }
            catch (Exception) { }
        }
    }
}